<?php

defined('BASEPATH') or die('forbidden');

/**
 * 验证码类
 * @author  Karson
 * @name    captcha.php
 * @since   2013-3-7 22:22:07
 */
class Captcha {

    var $length;
    var $font;
    var $size;
    var $angel;
    var $file;
    var $width;
    var $height;

    function Captcha() {
        $this->length = CAPTCHA_LENGTH;
        $this->font = CAPTCHA_FONT;
        $this->size = CAPTCHA_SIZE;
        $this->angel = CAPTCHA_ANGEL;
        $this->file = CAPTCHA_FILE;
        $this->file = is_array($this->file) ? $this->file[mt_rand(0, count($this->file) - 1)] : $this->file;
        $this->width = CAPTCHA_WIDTH;
        $this->height = CAPTCHA_HEIGHT;
    }

    /**
     * 设置对象指定属性的值
     * @param string $var 属性的名称
     * @param string $value 属性的值
     * @return bool 成功为true
     */
    function set($var, $value) {
        $this->$var = $value;
        if ($var == 'file')
            $this->file = is_array($value) ? $this->file[mt_rand(0, count($value) - 1)] : $value;
        return true;
    }

    /**
     * 生成随机字符
     * @return bool 成功时返回生成的字符
     */
    function text() {
        $string = '';
        //$key = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9');
        $key = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
        //支持中文验证码,请先修改contant.php中的CAPTCHA_FONT的值
        //$key = array('宠','辱','不','惊','静','看','庭','前','花','开','花','落','得','失','无','意','漫','望','天','外','云','卷','云','舒');
        $keynum = count($key) - 1;
        for ($i = 0; $i < $this->length; $i++) {
            $string .= (string) $key[mt_rand(0, $keynum)];
        }
        return ($string == '' || strlen($string) < $this->length) ? 'ABCD' : $string;
    }

    /**
     * 生成验证码
     */
    function get() {
        srand((double) microtime() * 1000000);
        $auth_num_k = md5(rand(0, 9999));
        $random = substr($auth_num_k, 17, 4);
        $random = strtoupper($random);
        //$random = $this->text();
        header('Content-type: image/png');
        session_start();
        session_register('captcha'); 
        if ($this->file != '' AND file_exists($this->file)) {
            $type = getimagesize($this->file);
            if ($type[2] == IMAGETYPE_GIF)
                $im = imagecreatefromgif($this->file);
            else if ($type[2] == IMAGETYPE_JPEG)
                $im = imagecreatefromjpeg($this->file);
            else if ($type[2] == IMAGETYPE_PNG)
                $im = imagecreatefrompng($this->file);
            $dest = imagecreatetruecolor($this->width, $this->height);
            imagecopy($dest, $im, 0, 0, 0, 0, $this->width, $this->height);
            $im = $dest;
        }else {
            $im = imagecreate($this->width, $this->height);
            $gray = imagecolorallocate($im, 255, 255, 255);
            imagefill($im, 0, 0, $gray); //采用区域填充法，设定（0,0）
            // 画干扰线
            for ($i = 0; $i < 5; $i++) {
                $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
                imagearc($im, mt_rand(- $this->width, $this->width), mt_rand(- $this->height, $this->height), mt_rand(30, $this->width * 2), mt_rand(20, $this->height * 2), mt_rand(0, 360), mt_rand(0, 360), $font_color);
            }
            // 画干扰点
            for ($i = 0; $i < 50; $i++) {
                $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
                imagesetpixel($im, mt_rand(0, $this->width), mt_rand(0, $this->height), $font_color);
            }
        }

        $fontcolor = imagecolorallocate($im, mt_rand(0, 50), mt_rand(0, 50), mt_rand(0, 50));
        for ($i = 0; $i < strlen($random); $i++) {
            $random_x = mt_rand(10, 12) + (14 * $i) - 10;
            $random_y = mt_rand(14, 20);
            $random_angle = mt_rand(-5, 8);
            imagettftext($im, $this->size, $random_angle, $random_x, $random_y, $fontcolor, $this->font, $random{$i});
        }

        imagepng($im);
        imagedestroy($im);
        //if (!isset($_SESSION))
            //session_start();
        $_SESSION['captcha'] = $random;
        exit;
    }

}